package _httplient;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

/**
 * @Author: huangya
 * @Date: 2019/11/7
 * @Description:
 */
public class HttpClientDemo {

    public static void main(String[] args) {
        CloseableHttpClient client = HttpClients.createDefault();//client实例初始化
        HttpGet get = new HttpGet("http://www.baidu.com");//请求初始化
        //请求执行，获取返回对象
        CloseableHttpResponse response = null;
        try {
            response = client.execute(get);
            System.out.println(response.getStatusLine().getStatusCode());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            try {
                response.close();//关闭返回对象
                client.close();//关闭client
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
